package com.nl.controller.xuyi.controller;

import com.alibaba.fastjson.JSON;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.model.BaseResult;
import com.nl.controller.xuyi.bean.xuyimap.*;
import com.nl.controller.xuyi.service.XuYiMapService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: zhangheng
 * @Desc:
 * @date: 15:16 2020/9/1
 * @Modified By:
 */
@RestController
@RequestMapping("/xuYiMap")
@Slf4j
@Validated
public class XuYiMapController {
    @Autowired
    private XuYiMapService xuYiMapService;
    /**
     * desc: 盱眙圈划后获取
     * @param req:  
     * @return: com.nl.controller.common.model.Response
     * @author zhangheng
     * @date 2020/9/1 15:41
     */
    @RequestMapping("/getStations")
    public BaseResult<XuYiGetStationsResp> qryLineInfo(@RequestBody XuYiGetStationsReq req){
        log.info("XuYiLineController qryLineInfo 点位信息查询 start request: "+ JSON.toJSONString(req));
        try {
            //圈画范围经纬度信息
            if(!StringUtils.isEmpty(req.getLongAndLatListInfo())) {
                List<String> longAndLatList = JSON.parseArray(req.getLongAndLatListInfo(), String.class);
                req.setLongAndLatList(longAndLatList);
            }
        } catch(Exception e) {
            throw new SmsException(e.getMessage());
        }
        try {
            //选中点位名称
            if(!StringUtils.isEmpty(req.getPointNames())) {
                List<String> pointNameList = JSON.parseArray(req.getPointNames(), String.class);
                req.setPointNameList(pointNameList);
            }
        } catch(Exception e) {
            throw new SmsException(e.getMessage());
        }
        BaseResult<XuYiGetStationsResp> result = xuYiMapService.xuYiGetStations(req);
        log.info("XuYiLineController qryLineInfo 点位信息查询 end response: "+ JSON.toJSONString(result));
        return result;
    }
    @RequestMapping("/queryRange")
    public BaseResult<List<QueryRangeResp>> queryRange(@Validated @RequestBody QueryRangeReq  req, BindingResult bindingResult){
        log.info("xuYiMap queryRange 点位信息查询 start request: "+ JSON.toJSONString(req));
        if(bindingResult.hasErrors()) {
            throw new SmsException(bindingResult.getFieldError().getDefaultMessage());
        }
        if(!StringUtils.isEmpty(req.getMgrIds())) {
            List<String> mgrIds = JSON.parseArray(req.getMgrIds(), String.class);
            req.setMgrIdList(mgrIds);
        }
        BaseResult<List<QueryRangeResp>> result = xuYiMapService.queryRange(req);
        log.info("xuYiMap queryRange 点位信息查询 end response: "+ JSON.toJSONString(result));
        return result;
    }
    /**
     * desc: 更新发送范围
     * @param req:
     * @param bindingResult:
     * @return: com.nl.controller.common.model.BaseResult<java.lang.Object>
     * @author zhangheng
     * @date 2020/9/14 17:06
     */
    @RequestMapping("/updateRange")
    public BaseResult<Object> updateRange(@Validated @RequestBody UpdateRangeReq req, BindingResult bindingResult){
        log.info("XuYiLineController updateRange 更新发送范围 start request: "+ JSON.toJSONString(req));
        if(bindingResult.hasErrors()) {
            throw new SmsException(bindingResult.getFieldError().getDefaultMessage());
        }
        BaseResult<Object> result = xuYiMapService.updateRange(req);
        log.info("XuYiLineController updateRange 更新发送范围 end response: "+ JSON.toJSONString(result));
        return result;
    }



}
